package com.xm.servlet.admin;

import com.xm.biz.BanksBiz;
import com.xm.biz.impl.BanksBizImpl;
import com.xm.po.Banks;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.List;

@WebServlet("/banke")
public class BankeServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String type = request.getParameter("type");
        //删除银行卡信息
        if (type.equals("bankeDelete")){
            bankeDelete(request,response);
        }
        //查询所有银行卡信息
        else if (type.equals("banke")){
            banke(request,response);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
    //删除银行卡信息
    public void bankeDelete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        req.setCharacterEncoding("utf-8");
        //获取id
        Integer id = Integer.valueOf(req.getParameter("id"));
        BanksBiz banksBiz=new BanksBizImpl();
        //获取该id的银行卡信息
        Banks byId = banksBiz.findById(id);
        //修改为删除状态 -1删除 1有效
        byId.setBankFlag(-1);
        //获取结果
        boolean re = banksBiz.update(byId);
        if (re){
            resp.sendRedirect("banks");
        }else {
            PrintWriter out = resp.getWriter();
            out.print("<script>alert('删除失败！');location.href='banks';</script>");
            out.flush();
            out.close();
        }
    }
    //查询所有银行卡信息
    public void banke(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        req.setCharacterEncoding("utf-8");
        BanksBiz banksBiz=new BanksBizImpl();
        List<Banks> list=banksBiz.getAll();
        req.setAttribute("list",list);
        req.getRequestDispatcher("admin/Banks.jsp").forward(req,resp);
    }
}
